#!/usr/bin/env python3
import json
import os
import sys

ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
WIKI_DIR = os.path.join(ROOT_DIR, "docs", "wiki")
ISSUES_DIR = os.path.join(ROOT_DIR, ".agent-society", "issues")

REQUIRED_WIKI_FILES = {
    "proposal-integration-final.md",
    "phase0-theory-whitepaper.md",
    "phase0-governance-charter.md",
    "phase1-sandbox-plan.md",
    "stakeholder-signoff-brief.md",
}

REQUIRED_ISSUE_LANES = {
    "theory",
    "runtime",
    "economy",
    "governance",
    "telemetry",
    "comms",
}

def fail(message: str) -> None:
    print(message, file=sys.stderr)
    sys.exit(1)

if not os.path.isdir(WIKI_DIR):
    fail(f"Missing wiki directory: {WIKI_DIR}")

existing_wiki_files = set(os.listdir(WIKI_DIR))
missing = REQUIRED_WIKI_FILES - existing_wiki_files
if missing:
    fail(f"Missing required wiki files: {sorted(missing)}")

if not os.path.isdir(ISSUES_DIR):
    fail(f"Missing issue directory: {ISSUES_DIR}")

missing_lanes = [lane for lane in REQUIRED_ISSUE_LANES if not os.path.isdir(os.path.join(ISSUES_DIR, lane))]
if missing_lanes:
    fail(f"Missing issue lanes: {missing_lanes}")

# Validate issue files have front matter
for lane in REQUIRED_ISSUE_LANES:
    lane_dir = os.path.join(ISSUES_DIR, lane)
    for fname in os.listdir(lane_dir):
        if not fname.endswith(".md"):
            continue
        path = os.path.join(lane_dir, fname)
        with open(path, "r", encoding="utf-8") as f:
            first_line = f.readline().strip()
            if first_line != "---":
                fail(f"Issue file missing front matter: {path}")

print(json.dumps({"status": "ok"}))
